package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.LoginDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserService;
import com.heima.utils.common.AppJwtUtil;
import com.heima.utils.common.MD5Utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;


@Service
@Transactional
@Slf4j
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements ApUserService {
    /**
     * app端登录功能
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login(LoginDto dto) {
        //1.参数校验
        //2.用户登入
        if (StringUtils.isNotEmpty(dto.getPassword()) && StringUtils.isNotEmpty(dto.getPassword())) {
            //判断账户是否存在
            ApUser apUser = getOne(Wrappers.<ApUser>lambdaQuery().eq(ApUser::getPhone, dto.getPhone()));
            if (apUser == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "账号不存在");
            }
            //判断账号密码是否正确
            //加盐
            String salt =apUser.getSalt();
            //MD5加密
            String password = DigestUtils.md5DigestAsHex((dto.getPassword()+salt).getBytes());
            ApUser user = getOne(Wrappers.<ApUser>lambdaQuery().eq(ApUser::getPhone, dto.getPhone()).eq(ApUser::getPassword, password));
            if (user != null) {
                //密码正确
                //取得token
                String token = AppJwtUtil.getToken(apUser.getId().longValue());
                //存储数据
                Map userMap = new HashMap();
                userMap.put("id", apUser.getId());
                userMap.put("name", apUser.getName());
                userMap.put("phone", apUser.getPhone());

                Map map = new HashMap();
                map.put("user", userMap);
                map.put("token", token);
                return ResponseResult.okResult(map);
            } else {
                //密码错误
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
        }
        //3.游客登入
        else if (StringUtils.isBlank(dto.getPassword()) && StringUtils.isBlank(dto.getPhone())) {
            String token = AppJwtUtil.getToken(0L);
            Map map = new HashMap();
            map.put("token", token);
            return ResponseResult.okResult(map);
            //缺少参数
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }


    }

}